package com.example.administrator.szb.fragments.fragment_forWD;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.WTContentActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.adapter.AskFragmentAdapter;
import com.example.administrator.szb.bean.AskBean;
import com.example.administrator.szb.bean.Business;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by stone on 2018/1/2.
 */

public class FragmentAskContent2 extends MVPBaseFragment {

    private SmartRefreshLayout refreshLayout;
    private View error_net_ll,contents;
    private View nodata_ll;
    private Button error_btn;
    private ListView listView;
    private AskFragmentAdapter adapter;
    private Business business;
    private boolean isFirstEnter=true;

    private int current_page = 1;//列表当前页
    private int per_page = 10;//默认展示条数

    private int type = 1;//1热门2最新3待回答(必填)
    private int business_id = 0;//全部业务0具体业务是具体业务的id
    private List<AskBean> lists;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentaskcontent,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    @Override
    protected void initView(View view) {

        refreshLayout = (SmartRefreshLayout)view.findViewById(R.id.refreshlayout);
        listView = (ListView) view.findViewById(R.id.listView);
        error_net_ll = view.findViewById(R.id.error_net_ll);
        error_btn = view.findViewById(R.id.home_fragment_button_djcs_error);
        nodata_ll = view.findViewById(R.id.nodata_ll);
        contents = view.findViewById(R.id.contents);

    }

    @Override
    protected void initEvent() {


        error_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //doReuest();
                contents.setVisibility(View.VISIBLE);
                error_net_ll.setVisibility(View.GONE);
                refreshLayout.autoRefresh();
                requestReferList(business_id,type,0,1);
            }
        });
        if (isFirstEnter) {
            isFirstEnter = false;
            refreshLayout.autoRefresh();

        }
        refreshLayout.setEnableLoadMore(true);//是否启用上拉加载功能
        refreshLayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                int p = current_page+1;
                requestReferList(business_id,type,1,p);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                QTLog.e("刷新");
                requestReferList(business_id,type,0,1);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), WTContentActivity.class);
                //SampleApplicationLike.getQueueInstance().cancelAll("tag");
                intent.putExtra("refer_id", lists.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {

        lists = new ArrayList<>();

    }
    /**
     * 执行业务请求
     * isRefreshOrMore :其中传入0代表刷新，1代表加载更多
     */
    private void requestReferList(int business_id, int type, final int isRefreshOrMore,int page) {
        nodata_ll.setVisibility(View.GONE);
        contents.setVisibility(View.VISIBLE);
        error_net_ll.setVisibility(View.GONE);
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        String id="0";
        if (SPUtils.getUserinfo()!=null){
            id=SPUtils.getUserinfo().getId()+"";
        }
        map.put("id",  id);
        map.put("business_id", business_id + "");
        map.put("type", type + "");
        map.put("current_page", page + "");
        map.put("per_page", per_page + "");
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.URL_WD2, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                str = str.replace("{}","[]");
                //AskPage listStr = JSON.parseObject(str,new TypeReference<AskPage>(){});
                List<AskBean> list = JSON.parseArray(str, AskBean.class);
                /*if (listStr.getList()==null){
                    listStr.setList("[]");
                }*/

                if (isRefreshOrMore==1){
                    //加载更多
                    refreshLayout.finishLoadmore();

                    current_page++;
                    if (list.size()<per_page){

                        //没有更多了
                        refreshLayout.setEnableLoadMore(false);

                    }
                }else {
                    lists.clear();

                    //刷新
                    refreshLayout.finishRefresh();
                    refreshLayout.setEnableLoadMore(true);
                    if (list.size()<per_page){

                        //没有更多了
                        refreshLayout.setEnableLoadMore(false);

                    }
                    current_page=1;
                }
                lists.addAll(list);
                if (lists.size()==0){
                    nodata_ll.setVisibility(View.VISIBLE);
                }
                initAdater();

            }

            @Override
            public void onFailure(int requestde, String str) {
                error_btn.setVisibility(View.VISIBLE);
                error_net_ll.setVisibility(View.VISIBLE);
                contents.setVisibility(View.GONE);
            }
        });
    }
    /**
     * 从FragmentAsk传来的值
     * @param b
     */
    public void setDatas(Business b,int t){
        this.type=t;
        this.business = b;
        business_id = this.business.getId();
    }

    private void initAdater(){
        if (adapter==null){
            adapter = new AskFragmentAdapter(context,lists);
            listView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }
}
